<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input
        v-model="params.name"
        placeholder="请输入内容"
        class="search-main"
      />
      <el-button type="primary" @click="cx">查询</el-button>
    </div>
    <!-- 添加楼宇 -->
    <el-button
      style="margin: 16px"
      type="primary"
      @click="addBuilding"
    >添加楼宇</el-button>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="builForm">
        <el-table-column type="index" label="序号" />
        <el-table-column label="楼宇名称" width="180" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" width="120" prop="area" />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column label="状态" prop="status" :formatter="formatter" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="bj(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="sc(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="currentChange"
      />
    </div>
    <!-- 弹框 -->
    <!-- 添加楼宇弹框 -->
    <el-dialog
      :title="addForm.id ? '修改楼宇' : '添加楼宇'"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <el-button size="mini" @click="dialogVisibl">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="confirmAdd"
        >确 定</el-button>
      </template></el-dialog>
  </div>
</template>

<script>
import { editBuildingListAPI, getBuildingListAPI, createBuildingListAPI, delBuildingListAPI } from '@/api/building'
export default {
  data() {
    return {
      bjname: '',

      params: {
        page: 1,
        pageSize: 5,
        name: null
      },
      builForm: [],
      total: 0,
      dialogVisible: false,
      // 弹框数据
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      // 弹框校验
      addFormRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [{ required: true, message: '请输入楼宇面积', trigger: 'blur' }],
        propertyFeePrice: [
          { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.getBuildingList()
  },
  methods: {
    async getBuildingList() {
      const res = await getBuildingListAPI(this.params)
      // console.log(res, '楼宇信息')
      this.builForm = res.data.rows
      this.total = res.data.total
    },
    formatter(row) {
      const MAP = {
        0: '闲置中',
        1: '租赁中'
      }
      // console.log(row, 'row')//每一行的数据
      return MAP[row.status]
    },
    currentChange(page) {
      // console.log(page)//页码
      this.params.page = page
      // 重新渲染
      this.getBuildingList()
    },
    /* 查询 */
    cx() {
      this.params.page = 1
      this.getBuildingList()
    },
    // 添加楼宇弹出弹框
    addBuilding() {
      this.dialogVisible = true
      this.addForm = {}
    },
    // 关闭弹框
    closeDialog() {
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
    },
    // 取消
    dialogVisibl() {
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
    },
    // 新增/修改
    confirmAdd() {
      this.$refs.addForm.validate(async(validate) => {
        if (validate) {
          // console.log('全部校验成功')
          if (this.addForm.id) {
            await editBuildingListAPI(this.addForm)
          } else {
            await createBuildingListAPI(this.addForm)
          }
          // console.log(res, '提交数据返回的数据')
          // this.$refs.addForm.resetFields()
          this.bjname = (this.addForm.id ? '更新成功' : '创建成功')
          this.dialogVisible = false
          this.getBuildingList()
        }
      })
    },
    // 删除
    async sc(row) {
      console.log(row)
      await delBuildingListAPI(row.id)
      this.getBuildingList()
    },
    // 编辑--弹弹框
    bj(row) {
      this.dialogVisible = true
      const { id, area, floors, name, propertyFeePrice } = row
      this.addForm = {
        id, area, floors, name, propertyFeePrice
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
</style>
